$(function () {
    var apiUrl = 'http://wechat.roromy.com/';
    var appId = 'wx8ddbb6c833ff7c30';
    var userData = localStorage.getItem('userData');
    if (userData === null) {
        var code = getQueryString('code');
        if (code === null) {
            var locUrl = location.href;
            location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId + '&redirect_uri=' + encodeURIComponent(locUrl) + '&response_type=code&scope=snsapi_base&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
        } else {
            $.ajax({
                type: 'get',
                url: apiUrl + 'wechat/shop/getOpenid',
                async: false,
                cache: false,
                data: {code: code},
                dataType: 'json',
                success: function (result) {
                    // alert(result.data);
                    var jsonStr = JSON.stringify(result.data);
                    localStorage.setItem('userData', jsonStr);
                    // if (result!=null && result.hasOwnProperty('openid') && result.openid!="") {
                    //
                    // } else { }
                }
            });
        }
    } else {
        alert(userData);
    }

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = location.search.substr(1).match(reg);
        if (r !== null) return unescape(decodeURI(r[2]));
        return null;
    }

});